package com.sk.cardeal.act.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sk.cardeal.R;
import com.sk.cardeal.act.home.activity.ApplyActivity;
import com.sk.cardeal.act.home.activity.BeautyActivity;
import com.sk.cardeal.act.home.activity.BusinessActivity;
import com.sk.cardeal.act.home.activity.CleanActivity;
import com.sk.cardeal.act.home.activity.CustomManageActivity;
import com.sk.cardeal.act.home.activity.FixActivity;
import com.sk.cardeal.act.home.activity.GetCarActivity;
import com.sk.cardeal.act.home.activity.HomePageActivity;
import com.sk.cardeal.act.home.activity.LoginActivity;
import com.sk.cardeal.act.home.activity.MessageBoardActivity;
import com.sk.cardeal.act.home.activity.MessageInfoActivity;
import com.sk.cardeal.act.home.activity.OrderManageActivity;
import com.sk.cardeal.act.home.activity.WorkFormManageActivity;
import com.sk.cardeal.act.settlement.SettlementListActivity;
import com.sk.cardeal.base.BaseFragment;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.dialog.OpenFormDialogFragment;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.utils.UpLoadUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/12/14.
 */

public class HomeFragment extends BaseFragment implements HandleDataCallBack, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.person_info)
    ImageView personInfo;
    @BindView(R.id.notice_icon)
    ImageView noticeIcon;
    @BindView(R.id.get_car)
    FrameLayout getCar;
    @BindView(R.id.open_form)
    FrameLayout openForm;
    @BindView(R.id.form_manage)
    FrameLayout formManage;
    @BindView(R.id.order_ll)
    FrameLayout orderLl;
    @BindView(R.id.clean_card)
    FrameLayout cleanCard;
    @BindView(R.id.message_board)
    FrameLayout messageBoard;
    @BindView(R.id.custom_manage)
    FrameLayout customManage;
    @BindView(R.id.count_analyse)
    FrameLayout countAnalyse;
    @BindView(R.id.apply_manage)
    FrameLayout applyManage;
    Unbinder unbinder;
    @BindView(R.id.order_num)
    TextView orderNum;
    @BindView(R.id.top_order)
    LinearLayout topOrder;
    @BindView(R.id.wait_num)
    TextView waitNum;
    @BindView(R.id.top_wait)
    LinearLayout topWait;
    @BindView(R.id.fixing_num)
    TextView fixingNum;
    @BindView(R.id.top_fixing)
    LinearLayout topFixing;
    @BindView(R.id.wait_pay_num)
    TextView waitPayNum;
    @BindView(R.id.top_wait_pay)
    LinearLayout topWaitPay;
    @BindView(R.id.form_manage_num)
    TextView formManageNum;
    @BindView(R.id.order_manage_num)
    TextView orderManageNum;
    @BindView(R.id.swipe_ll)
    SwipeRefreshLayout swipeLl;
    Unbinder unbinder1;
    private List<LocalMedia> pictureList = new ArrayList<>();
    private boolean isRefresh = false;

    @Override
    protected int getContentView() {
        return R.layout.fragment_home;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        swipeLl.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),getResources().getColor(R.color.colorPrimary));
        swipeLl.setOnRefreshListener(this);
        tvTip.setText("Hello  " + MyUtils.getUser(mContext).getLoginName());
        AppApi.getHomeDataCount(this, 0, getActivity());
    }

    @OnClick({R.id.home_cash, R.id.person_info, R.id.notice_icon, R.id.get_car, R.id.open_form, R.id.form_manage, R.id.order_ll, R.id.clean_card, R.id.message_board, R.id.custom_manage, R.id.count_analyse, R.id.apply_manage, R.id.top_order, R.id.top_wait, R.id.top_fixing, R.id.top_wait_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home_cash:
                Intent homeCashIntent = new Intent(mContext, SettlementListActivity.class);
                startActivity(homeCashIntent);
                break;
            case R.id.person_info:
                Intent personIntent = new Intent(mContext, HomePageActivity.class);
                startActivityForResult(personIntent, 200);
                break;
            case R.id.notice_icon:
                Intent noticeIntent = new Intent(mContext, MessageInfoActivity.class);
                startActivity(noticeIntent);
                break;
            case R.id.get_car:
                Intent intent = new Intent(mContext, GetCarActivity.class);
                startActivity(intent);
                break;
            case R.id.open_form:
//                TinkerInstaller.onReceiveUpgradePatch(getActivity().getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk");
                OpenFormDialogFragment openFormDialogFragment = new OpenFormDialogFragment();
                openFormDialogFragment.setMyCallBack(this);
                openFormDialogFragment.show(getFragmentManager(), "open");
                break;
            case R.id.form_manage:
                Intent formIntent = new Intent(mContext, WorkFormManageActivity.class);
                startActivity(formIntent);
                break;
            case R.id.order_ll:
                Intent orderIntent = new Intent(mContext, OrderManageActivity.class);
                startActivity(orderIntent);
                break;
            case R.id.clean_card:
                Intent cleanIntent = new Intent(mContext, CleanActivity.class);
                startActivity(cleanIntent);
                break;
            case R.id.message_board:
                Intent messageIntent = new Intent(mContext, MessageBoardActivity.class);
                startActivity(messageIntent);
                break;
            case R.id.custom_manage:
                Intent customIntent = new Intent(mContext, CustomManageActivity.class);
                startActivity(customIntent);
                break;
            case R.id.count_analyse:
                Intent bussinessIntent = new Intent(mContext, BusinessActivity.class);
                startActivity(bussinessIntent);
                break;
            case R.id.apply_manage:
                Intent applyIntent = new Intent(mContext, ApplyActivity.class);
                startActivity(applyIntent);
                break;
            case R.id.top_order:
                Intent topOrderIntent = new Intent(mContext, OrderManageActivity.class);
                startActivity(topOrderIntent);
                break;
            case R.id.top_wait:
                Intent topWaitIntent = new Intent(mContext, WorkFormManageActivity.class);
                startActivity(topWaitIntent);
                break;
            case R.id.top_fixing:
                Intent topFixingIntent = new Intent(mContext, WorkFormManageActivity.class);
                topFixingIntent.putExtra("type", 2);
                startActivity(topFixingIntent);
                break;
            case R.id.top_wait_pay:
                Intent topWaitPayIntent = new Intent(mContext, WorkFormManageActivity.class);
                topWaitPayIntent.putExtra("type", 3);
                startActivity(topWaitPayIntent);
                break;
        }


    }

    @Override
    public void clickView(int id) {
        switch (id) {
            case R.id.beauty_ll:
                Intent beautyIntent = new Intent(mContext, BeautyActivity.class);
                startActivity(beautyIntent);
                break;
            case R.id.fix_ll:
                Intent fixIntent = new Intent(mContext, FixActivity.class);
                startActivity(fixIntent);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    UpLoadUtils.uploadPicTask(selectList.get(0).getPath(), HomeFragment.this);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    break;
                case 200:
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                    break;
            }
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        if(isRefresh){
            isRefresh = true;
            swipeLl.setRefreshing(false);
        }
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject returnObj = jsonObject.getJSONObject("returnObject");
        orderNum.setText(String.valueOf(returnObj.getIntValue("pageType0")));
        waitNum.setText(String.valueOf(returnObj.getIntValue("pageType1")));
        fixingNum.setText(String.valueOf(returnObj.getIntValue("pageType2")));
        waitPayNum.setText(String.valueOf(returnObj.getIntValue("pageType3")));
        if (returnObj.getIntValue("pageType0") > 0) {
            orderManageNum.setVisibility(View.VISIBLE);
            orderManageNum.setText(String.valueOf(returnObj.getIntValue("pageType0")));
        } else {
            orderManageNum.setVisibility(View.GONE);
        }
    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        AppApi.getHomeDataCount(this, 0, getActivity());
    }
}
